<?php

namespace backend\models;

use Yii;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;

/**
 * This is the model class for table "permission".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $name
 * @property string $code
 * @property integer $parent_id
 * @property integer $number
 * @property string $created_at
 * @property string $updated_at
 * @property integer $status
 */
class Permission extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'permission';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'parent_id', 'number', 'status'], 'integer'],            
            [['name', 'code'], 'required'],
            [['created_at', 'updated_at'], 'safe'],
            [['name'], 'string', 'max' => 255],
            [['code'], 'string', 'max' => 200]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'Người tạo',
            'name' => 'Tên quyền',
            'code' => 'Mã quyền',
            'parent_id' => 'Root',
            'number' => 'Số thứ tự',
            'created_at' => 'Thời gian tạo',
            'updated_at' => 'Thời gian cập nhật',
            'status' => 'Trạng thái',
        ];
    }
    /*
     * get list parent
     */
    public function getArrayParent(){
        $arr = [0=>['id'=>0,'name'=>'Root']];
        $parents = Permission::find()
                ->select('id,name')
                ->where(['parent_id' => 0])                                
                ->all();
        foreach ($parents as $r){
            $arr[]=['id'=>$r['id'],'name'=>$r['name']];
        }
        return ArrayHelper::map($arr, 'id', 'name');
    }
    /*
     * get all permission of system 
     */
    public function loadArrayAllPermission(){
        $data = [];
        $parents = Permission::find()
                ->select('id,name')
                ->where(['parent_id' => 0])                                
                ->all();
        foreach ($parents as $r){
            $data[]=['id'=>$r['id'],'name'=>$r['name'],'CHID'=>self::getArrayAllChid($r['id'])];
        }
        return $data;
    }
    public function getArrayAllChid($parent_id){
        $data = [];
        $chids = Permission::find()
                ->select('id,name')
                ->where(['parent_id' =>$parent_id])                                
                ->all();
        foreach ($chids as $c){
            $data[]=['id'=>$c['id'],'name'=>$c['name']];
        }
        return $data;
    }
    /*
     * check permission of user 
     */
    public function checkPermision($code){
        //code 
        $slq="select permission.id,permission.code,user_permission.user_id from permission  LEFT JOIN user_permission ON  permission.id = user_permission.permission_id where permission.code=\"$code\" and user_permission.user_id=".Yii::$app->user->id;
        $data = Yii::$app->db->createCommand($slq)->queryAll();
        if(count($data) >0){
            return TRUE;
        }else{
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
}
